package com.loongtech.bi.manager.player;

import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class LeftManager extends TypeLeftManager<Object> {
	private static final long serialVersionUID = 1L;
	
	/**
	 * 根据开始结束时间和类型及类型，查询所有留存数据
	 * @param dateStart
	 * @param dateEnd
	 * @return
	 */
	public <T> List<T> getLeftByDate(Date dateStart, Date dateEnd, Class<T> c) {
		return findByQuery("from " + c.getSimpleName() + " where 1=1 and day>=:dayStart and day<=:dayEnd and loginusernum is not null and usercreatenum is not null order by day ", "dayStart", dateStart, "dayEnd", dateEnd);
	}

    /**
     * 根据开始结束时间和类型及类型，查询所有留存数据
     * @param dateStart
     * @param dateEnd
     * @return
     */
    public <T> List<T> getLeftByDate2(Date dateStart, Date dateEnd, Class<T> c) {
        return findByQuery("from " + c.getSimpleName() + " where 1=1 and day>=:dayStart and day<=:dayEnd and serverid='0' and loginusernum is not null and usercreatenum is not null order by day ", "dayStart", dateStart, "dayEnd", dateEnd);
    }


    /**
     * 根据开始结束时间和区服，查询所有留存数据
     * @param dateStart
     * @param dateEnd
     * @return
     */
    public <T> List<T> getLeftByDateByTag(String serverId, Date dateStart, Date dateEnd, Class<T> c) {
        return findByQuery("from " + c.getSimpleName() + " where serverid = ? and day>=? and day<=? and loginusernum is not null and usercreatenum is not null order by day ", serverId, dateStart, dateEnd);
    }

    public List<Object[]> getDayLeftByAllKind(Date dateStart, Date dateEnd,Class<?> c,String queryPlatform, String queryChannel, String queryServer) {
        return findByQuery("select date_format(day,'%Y-%m-%d'),loginusernum * 1.0 / usercreatenum from " + c.getSimpleName() + " where day>=:dayStart and day<=:dayEnd and platformid = '"+queryPlatform+"' and channelid = '"+queryChannel+"' and serverid = '"+queryServer+"' and loginusernum is not null and usercreatenum is not null group by day order by day desc", "dayStart", dateStart, "dayEnd", dateEnd);
    }
	
	/**
	 * 根据开始结束时间及类型，求留存率
	 * @param dateStart
	 * @param dateEnd
	 * @return
	 */
	public List<Object[]> getLeftByAllKind(Date dateStart, Date dateEnd,Class<?> c,String queryPlatform, String queryChannel, String queryServer) {
		return findByQuery("select date_format(day,'%Y-%m-%d'),sum(loginusernum) * 1.0 /sum(usercreatenum) from " + c.getSimpleName() + " where day>=:dayStart and day<=:dayEnd and platformid = '"+queryPlatform+"' and channelid = '"+queryChannel+"' and serverid = '"+queryServer+"' and loginusernum is not null and usercreatenum is not null group by day order by day desc", "dayStart", dateStart, "dayEnd", dateEnd);
	}
	
	/**
	 * 根据开始结束时间及类型，求留存率
	 * @param dateStart
	 * @param dateEnd
	 * @return
	 */
	public List<Object[]> getLeftByPlatformGroupByPlatform(Date dateStart, Date dateEnd,Class<?> c) {
		return findByQuery("select platformid,sum(loginusernum) * 1.0 /sum(usercreatenum) from " + c.getSimpleName() + " where day>=:dayStart and day<=:dayEnd and platformid != '0' and loginusernum is not null and usercreatenum is not null group by platformid", "dayStart", dateStart, "dayEnd", dateEnd);
	}
	
	/**
	 * 根据开始结束时间及类型，求留存率
	 * @param dateStart
	 * @param dateEnd
	 * @return
	 */
	public List<Object[]> getLeftByChannelGroupByChannel(Date dateStart, Date dateEnd,Class<?> c) {
		return findByQuery("select channelid,sum(loginusernum) * 1.0 /sum(usercreatenum) from " + c.getSimpleName() + " where day>=:dayStart and day<=:dayEnd and channelid != '0' and loginusernum is not null and usercreatenum is not null group by channelid", "dayStart", dateStart, "dayEnd", dateEnd);
	}
}
